package com.jintian.smart.kernel.flowable.controller;

import com.jintian.smart.kernel.core.domain.PageQuery;
import com.jintian.smart.kernel.core.web.RestResultWrapper;
import com.jintian.smart.kernel.flowable.domain.query.DeployVoQuery;
import com.jintian.smart.kernel.flowable.domain.view.DeployVo;
import com.jintian.smart.kernel.flowable.domain.view.DeploymentVo;
import com.jintian.smart.kernel.flowable.service.DeployService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.beetl.sql.core.page.PageResult;
import org.flowable.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.text.ParseException;

@RestResultWrapper
@RestController
@RequestMapping("/flow/deploy")
@Schema(description = "流程模板及部署")
@Transactional
public class DeploymentController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private DeployService deployService;

    /**
     * 直接使用文件发布流程
     *
     * @throws IOException
     * @throws XMLStreamException
     */
    @Operation(summary = "流程模板文件")
    @Parameters(value = {@Parameter(name = "file", in = ParameterIn.DEFAULT, description = "模板文件")})
    @RequestMapping(value = "/file", method = RequestMethod.POST)
    public DeploymentVo publishInputStream(MultipartFile file) throws IOException, XMLStreamException {
        return deployService.deployByInputStream(file);
    }

    /**
     * 根据流程定义发布流程
     */
    @Operation(summary = "部署流程模板文件")
    @Parameters(value = {@Parameter(name = "id", in = ParameterIn.DEFAULT, description = "流程Id")})
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public DeploymentVo publish(@RequestParam(value = "id", required = true) String id) {
        return deployService.publish(id);
    }

    /**
     * 查询流程部署列表
     */
    @Operation(summary = "查询流程部署列表")
    @RequestMapping(path = "/list", method = RequestMethod.POST)
    public PageResult<DeployVo> list(@RequestBody PageQuery<DeployVo, DeployVoQuery> pageDTO) throws ParseException {
        return deployService.queryPageList(pageDTO);
    }

    /**
     * 查询流程部署版本列表
     */
    @Operation(summary = "查询流程部署版本列表")
    @RequestMapping(path = "/publishList", method = RequestMethod.POST)
    public PageResult<DeployVo> publishList(@RequestBody PageQuery<DeployVo, DeployVoQuery> pageDTO) {
        return deployService.queryPublishList(pageDTO);
    }

    /**
     * 获取流程配置信息
     */
    @Operation(summary = "获取流程配置信息")
    @Parameters(value = {@Parameter(name = "definitionId", in = ParameterIn.DEFAULT, description = "流程定义Id")})
    @PostMapping(path = "/bpmnXml")
    public String getBpmnXml(@RequestParam String definitionId) {
        return deployService.queryBpmnXmlById(definitionId);
    }
}
